package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.SmsHomeRecommendSubjectApi;
import com.itcat.mapper.SmsHomeRecommendSubjectMapper;
import com.itcat.mysql.pojo.SmsHomeBrand;
import com.itcat.mysql.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeRecommendSubjectApiImpl implements SmsHomeRecommendSubjectApi {
    @Autowired
    private SmsHomeRecommendSubjectMapper smsHomeRecommendSubjectMapper;

    //分页查询推荐品牌
    @Override
    public Page<SmsHomeRecommendSubject> list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(subjectName!=null,SmsHomeRecommendSubject::getSubjectName,subjectName)
                .eq(recommendStatus!=null,SmsHomeRecommendSubject::getRecommendStatus,recommendStatus);
        Page<SmsHomeRecommendSubject> subjectPage = smsHomeRecommendSubjectMapper.selectPage(page, wrapper);

        return subjectPage;
    }

    //添加首页推荐品牌
    @Override
    public void create(List<SmsHomeRecommendSubject> subjects) {

        //循环添加
        for (SmsHomeRecommendSubject subject : subjects) {
            smsHomeRecommendSubjectMapper.insert(subject);
        }
    }

    //修改专题推荐排序
    @Override
    public void updateSort(Integer id, Integer sort) {
        SmsHomeRecommendSubject subject = new SmsHomeRecommendSubject();
        subject.setSubjectId(id);
        subject.setSort(sort);
        smsHomeRecommendSubjectMapper.updateById(subject);

    }

    //批量修改推荐状态
    @Override
    public void updateRecommendStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<SmsHomeRecommendSubject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SmsHomeRecommendSubject::getId,ids).set(SmsHomeRecommendSubject::getRecommendStatus,recommendStatus);
        SmsHomeRecommendSubject subject = new SmsHomeRecommendSubject();
        subject.setRecommendStatus(recommendStatus);
        smsHomeRecommendSubjectMapper.update(subject,updateWrapper);
    }

    //批量删除专题推荐
    @Override
    public void delete(List<Integer> ids) {
        smsHomeRecommendSubjectMapper.deleteBatchIds(ids);
    }

}
